package com.lvmama.service.rhino.topic.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lvmama.common.enums.TopicPVTypeEnum;
import com.lvmama.dao.rhino.topic.PositionAnalysisDao;
import com.lvmama.pojo.dto.rhino.topic.TopicFunnelVO;
import com.lvmama.pojo.entity.rhino.topic.*;
import com.lvmama.service.rhino.topic.PositionAnalysisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class PositionAnalysisServiceImpl implements PositionAnalysisService {

    @Autowired
    private PositionAnalysisDao positionAnalysisDao;

    @Override
    public List<PositionAnalysis> queryDetailByCode(PositionAnalysis positionVO) {
        log.info("queryDetailByCode start");
        List<PositionAnalysis> result = null;
        try {
            result = positionAnalysisDao.queryDetailByCode(positionVO);
        }catch (Exception e){
            log.error("queryDetailByCode error",e.getMessage(),e);
        }
        log.info("queryDetailByCode end");
        return result;
    }

    // 分页获取专题列表
    @Override
    public PageInfo<PositionAnalysis> getTopicList(PositionAnalysis sc) {
        log.info("getTopicList start");
        PageHelper.startPage(sc.getCurrentPage(), sc.getPage_size());
        List<PositionAnalysis> positionAnalysis = null;
        PageInfo<PositionAnalysis> page = null;
        try {
            positionAnalysis = positionAnalysisDao.getAllList(sc);
            page = new PageInfo<PositionAnalysis>(positionAnalysis);
        }catch (Exception e){
            log.error("getTopicList error",e.getMessage(),e);
        }
        log.info("getTopicList end");
        return page;
    }

    // 获取坑位列表
    @Override
    public PageInfo<PositionAnalysis> getPitList(PositionAnalysis positionVO) {
        PageHelper.startPage(positionVO.getCurrentPage(), positionVO.getPage_size());
        List<PositionAnalysis> positionAnalysis = positionAnalysisDao.queryListByPit(positionVO);
        PageInfo<PositionAnalysis> page = new PageInfo<PositionAnalysis>(positionAnalysis);
        return page;
    }

    public List<PositionAnalysis> getPitListByCategory(PositionAnalysis positionVO) {
        if (StringUtils.isEmpty(positionVO.getPlatform())) {
            return positionAnalysisDao.queryThemeByCategory(positionVO);
        } else {
            return positionAnalysisDao.queryListByCategory(positionVO);
        }
    }


    @Override
    public List<PitPV> getPitPVByCategory(PositionAnalysis positionVO) {
        return positionAnalysisDao.getPitPVByCategory(positionVO);
    }

    @Override
    public List<PitPV> getAllPitPV(PositionAnalysis positionVO) {
        return positionAnalysisDao.getAllPitPV(positionVO);
    }

    @Override
    public PageInfo<PitPV> getTopicPVByCode(PositionAnalysis positionVO) {
        PageHelper.startPage(positionVO.getCurrentPage(), positionVO.getPage_size());
        List<PitPV> pitPVS = new ArrayList<PitPV>();
        if(positionVO.getName()== null){
            pitPVS = positionAnalysisDao.getTopicPVByCode(positionVO);
        }else{
            pitPVS = positionAnalysisDao.getTopicAllPVByCode(positionVO);
        }
        PageInfo<PitPV> page = new PageInfo<PitPV>(pitPVS);
        return page;
    }

    public List<PositionAnalysis> getPitListByDate(PositionAnalysis positionVO) {
        if (StringUtils.isEmpty(positionVO.getPlatform())) {
            return positionAnalysisDao.queryThemeByDate(positionVO);
        } else {
            return positionAnalysisDao.queryListByDate(positionVO);
        }
    }
    public List<PositionAnalysis> getPitBarByCategory(PositionAnalysis positionVO) {
        if (StringUtils.isEmpty(positionVO.getPlatform())) {
            return positionAnalysisDao.queryTheBarByCategory(positionVO);
        } else {
            return positionAnalysisDao.queryPitBarByCategory(positionVO);
        }
    }

    public PositionAnalysis getPageNumByCategory(PositionAnalysis positionVO) {
        if (StringUtils.isEmpty(positionVO.getPlatform())) {
            return positionAnalysisDao
                    .queryThemePageNumByCategory(positionVO);
        } else {
            return positionAnalysisDao.queryPageNumByCategory(positionVO);
        }
    }

    /**
     * 查询专题漏斗
     * @param query 查询条件
     * @return 结果集
     */
    @Override
    public List<SpecialTopicFunnelData> getTopicFunnel(TopicFunnelDataQuery query) {
        List<SpecialTopicFunnelData> dataList = new ArrayList<>();
        try {
            TopicFunnelVO topicFunnelVO = positionAnalysisDao.queryTopicFunnel(query);
            if(null != topicFunnelVO){
                for (TopicPVTypeEnum en : TopicPVTypeEnum.values()){
                    SpecialTopicFunnelData funnelData = new SpecialTopicFunnelData();
                    funnelData.setName(en.getName());
                    if(TopicPVTypeEnum.TOPIC_PAGE_SUM.getCode().equals(en.getCode())){
                        funnelData.setValue(topicFunnelVO.getTopicPageSum());
                    }else if(TopicPVTypeEnum.DETAIL_PAGE_SUM.getCode().equals(en.getCode())){
                        funnelData.setValue(topicFunnelVO.getDetailPageSum());
                    }else if (TopicPVTypeEnum.FILL_ORDER_PAGE_SUM.getCode().equals(en.getCode())){
                        funnelData.setValue(topicFunnelVO.getFillOrderPageSum());
                    }else if(TopicPVTypeEnum.PENDING_PAYMENT_PAGE_SUM.getCode().equals(en.getCode())){
                        funnelData.setValue(topicFunnelVO.getPendingPaymentPageSum());
                    }else if(TopicPVTypeEnum.COMPLETED_PAYMENT_PAGE_SUM.getCode().equals(en.getCode())){
                        funnelData.setValue(topicFunnelVO.getCompletedPaymentPageSum());
                    }
                    dataList.add(funnelData);
                }
            }
        } catch (Exception e) {
            return null;
        }
        return dataList;
    }
}
